<?php
/**
 * Created by PhpStorm.
 * User: 76683
 * Date: 2018/7/16
 * Time: 22:12
 */

namespace app\api\controller;


use think\Controller;
use think\Request;
use think\Response;
use app\api\until\Token;
use think\exception\HttpResponseException;

class Base extends Controller
{
    protected $code = null;

    protected $msg = null;

    protected $data = [];

    protected $uid = 0;

    protected $no_login = [
//        '小写'=>'小写',
        'wetopic' => ['index','thehot','thereply','hotlist'],
        'wecategory' => ['search','index','topiclist','thereply'],
        'wetopicdetail'=>[],
        'weusercategory'=>['index'],
        'login' => ['signin'],
        'wetopicdetail'=>['topicdetail'],
        'search'=>['topictitle','topiccontent']
    ];
    protected $token = null;

    function __construct(Request $request = null)
    {
        parent::__construct($request);
        $controller = strtolower($this->request->controller());
        $method = strtolower($this->request->action());
        $this->token = $this->request->post('token');
        $this->needLogin($controller, $method);
        if($this->token){
            $this->checkToken();
        }
    }

    /**
     * 判断是否需要登陆
     * @param $controller
     * @param $method
     */
    public function needLogin($controller, $method)
    {
        $arr = &$this->no_login;
        $key = strtolower($controller);
        if (isset($arr[$key])) {
            if (!in_array($method, $arr[$key])) {
                $this->checkToken();
            }
        }else{
            $this->checkToken();
        }
    }

    /**
     * token检查
     */
    public function checkToken()
    {
        $token = &$this->token;
        if(!$token){
            $this->code = 10000;
            $this->msg = '请登录';
            $this->__destruct();
        }
        $tokenModel = new Token();

        /**
         * 验证token是否过期
         */
//        $result = $tokenModel->validateTokenIsExpired($token);
//        if ($result) {
//            $this->code = 0;
//            $this->msg = 'token过期';
//            $this->__destruct();
//        } else {
//            $this->code = 0;
//            $this->msg = 'token错误';
//            $this->__destruct();
//        }
        //解析token
        $res = $tokenModel->decodeToken($token);
        if ($res === false) {
            list($this->code, $this->msg, $this->data) = [10000, '请登录', []];
            $this->__destruct();
        } else {
            $this->uid = $res;
        }
    }

    public function __destruct()
    {
        // TODO: Implement __destruct() method.
        $responseData = [
            'code' => $this->code,
            'msg' => $this->msg,
            'data' => $this->data,
            'timestamp' => time()
        ];
        $type = 'json';
        $headers = ['content-type' => 'application/json'];
        $response = Response::create($responseData, $type)->header($headers);
        throw new HttpResponseException($response);
    }

    public function __get($name)
    {
        // TODO: Implement __get() method.
        return isset($this->$name) ? $this->$name : '';
    }
}